﻿using PBO.Data;
using PBO.Data.PokemonOnline;
using PBO.Data.SQLite;
using PBO.Data.SQLite.DAL;
using PBO.Data.Util;
using PBO.Forms.DataForm;
using PBO.Forms.NetworkForm.BattleForm;
using PBO.Forms.NetworkForm.BattleForm.FourPlayer;
using PBO.Game.BattleClass;
using PBO.Game.Img;
using PBO.Network;
using PBO.Network.Enumerations;
using PBO.RoomClient;
using PBO.Util;
using PokemonBattle.BattleRoom.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PBO
{
    public partial class MainForm : Form
    {

        #region Instance

        private static MainForm instance;

        public static MainForm GetInstance()
        {
            if (instance == null || instance.IsDisposed)
                instance = new MainForm();
            return instance;
        }

        #endregion

        #region props

        public TeamData Team { get; set; }

        #endregion

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //var list = MyBox.GetList(1, 0, 0, 0, 1, 10, "spe", false);
            var rnd = new Random(unchecked((int)DateTime.Now.Ticks));
            this.Team = GameService.GetRandomTeam(rnd);
            观看录象RToolStripMenuItem.Visible = false;
            自定义数据ToolStripMenuItem.Visible = false;
            IndexForm.GetInstance(this).Show();
        }

        #region MainMenu

        private void 精灵资料PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var form = FrmDataDex.GetInstance();
            form.tabDexs.SelectedIndex = 0;
            form.Show();
        }

        private void 技能ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var form = FrmDataDex.GetInstance();
            form.tabDexs.SelectedIndex = 1;
            form.Show();
        }

        private void tsmAbility_Click(object sender, EventArgs e)
        {
            var form = FrmDataDex.GetInstance();
            form.tabDexs.SelectedIndex = 2;
            form.Show();
        }

        private void tsmItem_Click(object sender, EventArgs e)
        {
            var form = FrmDataDex.GetInstance();
            form.tabDexs.SelectedIndex = 3;
            form.Show();
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm.GetInstance().Show();
        }

        #endregion

        #region BuildBattleForm

        private RoomClientForm _roomForm;

        public RoomClientForm BuildRoomUserForm(User user, string address, string roomName)
        {
            //user.CustomDataInfo = Team.CustomInfo.DataName;
            //user.CustomDataHash = Team.CustomInfo.DataHash;
            RoomClientForm form = new RoomClientForm(user, address, roomName, Application.ProductVersion);
            form.MdiParent = this;
            form.Icon = Properties.Resources.PokemonBall;
            form.OnBuildBattleAgentForm += BuildBattleAgentForm;
            form.OnBuildBattleServerForm += BuildBattleServerForm;
            form.OnBuildBattleClientForm += BuildBattleClientForm;
            form.OnBuildBattleObserverForm += BuildBattleObserverForm;
            form.OnBuildFourPlayerForm += BuildAgent4PClientForm;
            form.FormClosed += RoomForm_FormClosed;
            _roomForm = form;
            return form;
        }

        private void RoomForm_FormClosed(object sender, EventArgs e)
        {
            _roomForm = null;
        }

        public BattleServerForm BuildBattleServerForm(string name, BattleMode mode, BattleRule[] rules)
        {
            BattleServerForm form = new BattleServerForm(this.Team, name, mode, rules);
            form.MdiParent = this;
            return form;
        }
        public BattleClientForm BuildBattleClientForm(string address, byte position, string name, BattleMode mode)
        {
            BattleClientForm form = new BattleClientForm(address, name, this.Team, mode, position);
            form.MdiParent = this;
            return form;
        }
        public AgentClientForm BuildBattleAgentForm(AgentBattleInfo battleInfo)
        {
            AgentClientForm form = new AgentClientForm(battleInfo, this.Team);
            form.MdiParent = this;
            return form;
        }

        public BattleObserverForm BuildBattleObserverForm(string address, byte position)
        {
            BattleObserverForm form = new BattleObserverForm(address, position);
            form.MdiParent = this;
            return form;
        }
        public BattleObserverForm BuildBattleObserverForm(int identity, string address, byte position)
        {
            BattleObserverForm form = default(BattleObserverForm);
            if (identity == -1)
            {
                form = new BattleObserverForm(address, position);
            }
            else
            {
                form = new AgentObserverForm(identity, address, position);
            }
            form.MdiParent = this;
            return form;
        }

        public FourPlayerServerForm Build4PServerForm(string name, BattleRule[] rules)
        {
            FourPlayerServerForm form = new FourPlayerServerForm(name, rules);
            form.MdiParent = this;
            return form;
        }
        public FourPlayerClientForm Build4PClientForm(string address, string name)
        {
            FourPlayerClientForm form = new FourPlayerClientForm(address, name);
            form.MdiParent = this;
            return form;
        }
        public FourPlayerClientForm BuildAgent4PClientForm(int identity, string address, string name, bool asHost, FourPlayerFormCallback callback)
        {
            AgentFourPlayerForm form = new AgentFourPlayerForm(identity, address, name, asHost, callback);
            form.MdiParent = this;
            return form;
        }

        #endregion

    }
}
